/**
 * @author 董一华
 * @since 1.00
 * @version 1.00
 * 
 * 品牌下拉框控件
 * 使用说明：
 * jsp中使用范例(最短模式)：<htdz-tag:SelectBrandTag />
 * 			   (最常用模式)：<htdz-tag:SelectBrandTag selected="${xxx}"/>
 * 参数说明：
 * 
 * 1. name可以不写,缺省值为"brandcode"
 * 2. id可以不写,缺省值为name的值
 * 3. onchange可以不写；
 * 4. firstoption为首个下拉框的text。可以不写，缺省值为"全部"；
 *    如果firstoption为MyConstant.SELECT_FIRST_OPTION_NO，则表示此下拉框不需要添加额外option
 * 5. firstvalue为首个下拉框的value。可以不写，缺省值为"MyConstant.SELECT_FIRST_VALUE"    
 * 6. selected为下拉框选中的值，可以不写。
 * 7. range为product取值范围，可选项，缺省值为now。可以有以下取值:
 *    (1)all：所有productgroup中的产品
 *    (2)valid：productgroup中status<>'0'的产品
 *    (3)now：productgroup中nowflag='1'的产品
 *    (4)rgs：orgacode相应的productrgs的产品
 *    (5)depot:orgacode库存产品
 *    (6)vendor:orgacode厂家所有机型
 * 8. orgacode为可选项，如果range为rgs或depot或vendor时，orgacode为必填项。
 * 9. hidedesc：是否隐藏文字说明“品牌：”，缺省值为显示
 * 
 * 
 * 修订列表： v1.00 2010-03-20 
 * 			修订人：董一华 
 * 			修订内容：初始版本
 * 
 * 			v1.02 2010-06-05 
 * 			修订人：董一华 
 * 			修订内容：新增：hidedesc
 * 
 *          v1.03 2010-07-24 
 * 			修订人：董一华 
 * 			修订内容：新增：orgacode
 */

package com.zjht.gmcc.bean.tag;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.zjht.gmcc.bean.util.MyConstant;
import com.zjht.gmcc.bean.util.MyJDBC;
import com.zjht.gmcc.bean.util.MyParam;

public class SelectProvideType extends TagSupport {
	private static final long serialVersionUID = 7343412033252888637L;
	private String name;
	private String id;
	private String firstoption;
	private String firstvalue;
	private String onchange;
	private String selected;
	private String parentid;
	private boolean disabled;


	public SelectProvideType() {
	}

	@SuppressWarnings("static-access")
	public int doStartTag() throws JspException {
		String name = this.name;
		String id = this.id;
		String firstoption = this.firstoption;
		String firstvalue = this.firstvalue;
		String onchange = this.onchange;
		String selected = this.selected;
		String range = this.parentid;
	    boolean disabled=this.disabled;
	
		if (name == null || name.length() == 0) {
			name = "parameter";
		}
		if (id == null || id.length() == 0) {
			id = name;
		}
		
		
		StringBuilder html = new StringBuilder();
		
		html.append("<select name=\"");
		html.append(name);
		html.append("\"	id=\"");
		html.append(id);
		html.append("\" ");
		if (onchange != null && onchange.length() > 0) {
			html.append("onchange=\"");
			html.append(onchange);
			html.append("\"");
		}
		if(disabled)//禁用
			html.append(" disabled ");
		
		html.append(">");
		if (!MyConstant.SELECT_FIRST_OPTION_NO.equals(firstoption))// 如果firstoption为MyConstant.SELECT_FIRST_OPTION_NO，则不需要添加额外的option
		{
			if (firstoption == null || firstoption.length() == 0)
				firstoption = "全部";

			if (firstvalue == null || firstvalue.length() == 0)
				firstvalue = MyConstant.SELECT_FIRST_VALUE;

			html.append("<option value=\"");
			html.append(firstvalue);
			html.append("\">");
			html.append(firstoption);
			html.append("</option>");
		}
		try {
			String strSQL = "";

			MyJDBC myJDBC = null;
			try {
				myJDBC = new MyJDBC();
				strSQL = "select paraid,paraname from parameter where parentid=? and status='0' ORDER BY listnum ";
				
				
				PreparedStatement ps = myJDBC.prepareStatement(strSQL);
				ps.setString(1, parentid);
				
				ResultSet rs = ps.executeQuery();
				String brandcode_tmp = "";
				while (rs.next()) {
					brandcode_tmp = rs.getString("paraid");
					html.append("<option value=\"");
					html.append(brandcode_tmp);
					html.append("\" ");
					if (selected != null && selected.length() > 0
							&& brandcode_tmp.equals(selected))
						html.append("selected");
					html.append(">");
					html.append(rs.getString("paraname"));
					html.append("</option>");
				}
				rs.close();
				rs = null;
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				MyJDBC.close(myJDBC);
			}
		
			html.append("</select>");

			pageContext.getOut().print(html.toString());
		} catch (Exception e) {
			throw new JspException(e.getMessage());
		}
		html=null;
		return this.SKIP_BODY;
	}

	public void setName(String newName) {
		name = newName;
	}

	public void setId(String newId) {
		id = newId;
	}

	public void setFirstoption(String firstoption) {
		this.firstoption = firstoption;
	}

	public void setOnchange(String onchange) {
		this.onchange = onchange;
	}

	public void setSelected(String selected) {
		this.selected = selected;
	}

	public void setFirstvalue(String firstvalue) {
		this.firstvalue = firstvalue;
	}


	public String getParentid() {
		return parentid;
	}

	public void setParentid(String parentid) {
		this.parentid = parentid;
	}

	public boolean isDisabled() {
		return disabled;
	}

	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}

   


}
